//
//  LGCancerSymptomDetailCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/22.
//

#import "LGCancerSymptomDetailCell.h"
#import "LGCheckPhotoTool.h"
@interface LGCancerSymptomDetailCell()
/**   */
@property (nonatomic , strong)UIView *tipView;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)NSMutableArray *tipArray;
/**   */
@property (nonatomic , strong)NSMutableArray *photoArray;
/**   */
@property (nonatomic , strong)NSArray *picArray;
/**   */
@property (nonatomic , assign)NSInteger count;
/**   */
@property (nonatomic , assign)CGFloat photoW;
/**   */
@property (nonatomic , assign)CGFloat photoH;

@end

@implementation LGCancerSymptomDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.count = isPad?8:4;
        self.photoW = (Screen_W-viewPix(56)-viewPix(8)*(self.count-1))/self.count;
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    //内容
    NSArray *tipArray = dataDic[@"content"];
    for (UILabel *label in self.tipArray) {
        [label removeFromSuperview];
    }
    [self.tipArray removeAllObjects];
    
    CGFloat tipTop = 0;
    for (NSInteger i=0; i<tipArray.count; i++) {
        [self createTipLabel:tipArray[i] top:tipTop];
        tipTop+=viewPix(30);
    }
    tipTop = (tipTop>0)?tipTop-viewPix(5):tipTop;
    [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(tipTop));
    }];
    
    //图片
    NSArray *itemArray = dataDic[@"images"];
    for (UIImageView *imageView in self.photoArray) {
        [imageView removeFromSuperview];
    }
    self.photoH = 0;
    [self.photoArray removeAllObjects];
    NSMutableArray *picArray = [NSMutableArray array];
    for (NSInteger i=0; i<itemArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12)+(self.photoW+viewPix(8))*(i%self.count), (self.photoW+viewPix(8))*(i/self.count), self.photoW, self.photoW)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:itemArray[i]]];
        imageView.userInteractionEnabled = YES;
        imageView.tag = 1000+i;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkBigPhotoAction:)]];
        [self.photoView addSubview:imageView];
        [self.photoArray addObject:imageView];
        [picArray addObject:LGNSString(itemArray[i])];
        self.photoH = kMaxY(imageView.frame);
    }
    self.picArray = picArray;
    CGFloat height = (itemArray.count>0)?self.photoH:0;
    CGFloat photoTop = (itemArray.count>0)?viewPix(8):0;
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipView.mas_bottom).offset(photoTop);
        make.height.equalTo(@(height));
    }];
}



-(void)checkBigPhotoAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView = (UIImageView *)sender.view;
    NSInteger index = imageView.tag-1000;
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"患者报告";
    bigPhotoView.picArray = self.picArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:index];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.type = LGPositionTypeSingle;
    [self.baseView addSubview:self.tipView];
    [self.baseView addSubview:self.photoView];
   
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(12));
        make.left.right.equalTo(self.baseView);
        make.height.equalTo(@(0));
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipView.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.tipView);
        make.bottom.equalTo(self.baseView).offset(-viewPix(10));
        make.height.equalTo(@(0));
    }];
}

-(UIView *)tipView{
    if(!_tipView){
        _tipView = [[UIView alloc]init];
        _tipView.clipsToBounds = YES;
    }
    return _tipView;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc]init];
    }
    return _photoView;
}

-(NSMutableArray *)tipArray{
    if(!_tipArray){
        _tipArray = [NSMutableArray array];
    }
    return _tipArray;
}
-(NSMutableArray *)photoArray{
    if(!_photoArray){
        _photoArray = [NSMutableArray array];
    }
    return _photoArray;
}

-(void)createTipLabel:(NSDictionary *)tempDic top:(CGFloat)top{
    UILabel *titleLabel = [UILabel lableWithText:LGNSString(tempDic[@"title"]) colorString:@"#606266" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    [self.tipView addSubview:titleLabel];
    
    UILabel *valueLabel = [UILabel lableWithText:LGNSString(tempDic[@"value"]) colorString:@"#000000" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
    [self.tipView addSubview:valueLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipView).offset(top);
        make.left.equalTo(self.tipView).offset(viewPix(16));
        make.width.mas_greaterThanOrEqualTo(@(80));
    }];
    [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.left.equalTo(titleLabel.mas_right).offset(viewPix(20));
    }];
    [self.tipArray addObject:titleLabel];
    [self.tipArray addObject:valueLabel];
}


@end
